<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发行人" prop="publishName">
        <el-input
          v-model="queryParams.publishName"
          placeholder="请输入发行人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分红状态" prop="dividendVerify">
        <el-select v-model="queryParams.dividendVerify" placeholder="请选择分红状态" clearable size="small">
          <el-option
            v-for="dict in dividendVerifyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="investdividendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发行人" align="center" prop="publishName" />
      <el-table-column label="分红金额" align="center" prop="dividendNumber" />
      <el-table-column label="分红信息" align="center" prop="dividendMessage" />
      <el-table-column label="分红状态" align="center" prop="dividendVerify" :formatter="dividendVerifyFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.dividendVerify === 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >确认</el-button>
          <el-button
            v-if="scope.row.dividendVerify === 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 确认分红收入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-descriptions :column="1" title="发行信息">
          <el-descriptions-item label="发行人">{{ form.publishName }}</el-descriptions-item>
          <el-descriptions-item label="分红金额">{{ form.dividendNumber }}</el-descriptions-item>
          <el-descriptions-item label="分红信息">{{ form.dividendMessage }}</el-descriptions-item>
        </el-descriptions>
        <div class="description-title margin-top">意见反馈</div>
        <div class="margin-top">
            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInvestdividend, getInvestdividend, delInvestdividend, addInvestdividend, updateInvestdividend, exportInvestdividend } from "@/api/invest/asset/investdividend";

export default {
  name: "Investdividend",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分红收入表格数据
      investdividendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分红状态(0未确认，1已确认)字典
      dividendVerifyOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        publishId: null,
        publishName: null,
        investId: null,
        investName: null,
        dividendId: null,
        dividendNumber: null,
        dividendMessage: null,
        dividendVerify: 1,
        deptId: null,
        createById: null,
        updateById: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("dividend_verify").then(response => {
      this.dividendVerifyOptions = response.data;
    });
  },
  methods: {
    /** 查询分红收入列表 */
    getList() {
      this.loading = true;
      listInvestdividend(this.queryParams).then(response => {
        this.investdividendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分红状态(0未确认，1已确认)字典翻译
    dividendVerifyFormat(row, column) {
      return this.selectDictLabel(this.dividendVerifyOptions, row.dividendVerify);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        publishId: null,
        publishName: null,
        investId: null,
        investName: null,
        dividendId: null,
        dividendNumber: null,
        dividendMessage: null,
        dividendVerify: null,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分红收入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInvestdividend(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分红收入";
      });
    },
    //进行确认
    submit() {
      this.form.dividendVerify = "2";
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateInvestdividend(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除分红收入编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInvestdividend(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有分红收入数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInvestdividend(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped>

  .margin-top {
    margin-top: 20px;
  }

  .description-title {
    font-size: 16px;
    font-weight: bold;
  }
</style>
